package com.hengxinguotong.baselibrary.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.hengxinguotong.baselibrary.R;
import com.hengxinguotong.baselibrary.wheel.WheelView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by hjz on 2018/4/10.
 */

public class SelectDialog<T> extends Dialog
{
    private Context context;
    private WheelView wheelView;
    private List<T> iTextList;
    private SelectListener selectListener;

    public SelectDialog(@NonNull Context context, List<T> iTextList, SelectListener selectListener)
    {
        super(context, R.style.dialog_no_action_bar);
        this.context = context;
        if (iTextList == null)
        {
            this.iTextList = new ArrayList<>();
        } else
        {
            this.iTextList = iTextList;
        }
        this.selectListener = selectListener;
    }

    public SelectDialog(@NonNull Context context, int themeResId)
    {
        super(context, themeResId);
    }

    protected SelectDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener)
    {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select);
        TextView dialogSelectConfirm = findViewById(R.id.dialog_select_confirm);
        dialogSelectConfirm.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                dismiss();
                int index = wheelView.getCurrentItem();
                T t = iTextList.get(index);
                selectListener.onSelect(t, index);
            }
        });
        TextView dialogSelectCancel = findViewById(R.id.dialog_select_cancel);
        dialogSelectCancel.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                dismiss();
            }
        });
        wheelView = findViewById(R.id.wheel_view);
        wheelView.setVisibleItems(8);
        ITextAdapter iTextAdapter = new ITextAdapter(context, iTextList);
        wheelView.setViewAdapter(iTextAdapter);
        setCancelable(true);
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);
        window.setWindowAnimations(R.style.dialog_select_theme);
    }

    public interface SelectListener<T>
    {
        void onSelect(T t, int index);
    }
}
